package com.hoshiicloud.registered.utils;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.constant.TokenUserFieldEnum;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.utils.token.JWTUtil;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;

/**
 * @author qry
 * @date 2019/8/8 16:02
 */
public class UserInfoUtil {
    public static TokenUsersVO getUserInfo(HttpServletRequest rq){
        String token = rq.getHeader("X-Token");
        if(StringUtils.isBlank(token)){
            throw new ServiceException("登录信息有误");
        }
        DecodedJWT jwt= JWTUtil.getDecodedJWT(token);
        if(jwt==null){
            throw new ServiceException("登录信息有误");
        }
        TokenUsersVO userInfo= new TokenUsersVO();
        userInfo.setUsername(jwt.getClaim(TokenUserFieldEnum.USERNAME.getName()).asString());
        userInfo.setRealName(jwt.getClaim(TokenUserFieldEnum.REAL_NAME.getName()).asString());
        userInfo.setUserType(jwt.getClaim(TokenUserFieldEnum.USER_TYPE.getName()).asString());
        userInfo.setStoreId(jwt.getClaim(TokenUserFieldEnum.STORE_ID.getName()).asLong());
        userInfo.setPlatId(jwt.getClaim(TokenUserFieldEnum.PLAT_ID.getName()).asLong());
        userInfo.setCompanyId(jwt.getClaim(TokenUserFieldEnum.COMPANY_ID.getName()).asLong());
        userInfo.setUserId(jwt.getClaim(TokenUserFieldEnum.USER_ID.getName()).asLong());
        return userInfo;
    }
}
